package com.manor.paradise.dialog

import android.content.Context
import android.graphics.Color
import android.text.*
import android.text.method.PasswordTransformationMethod
import android.text.style.ForegroundColorSpan
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.CommonAppConfig
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.bean.Record
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_shopinfo_popup.view.*
import java.math.BigDecimal

/**
 * 集市交易
 * @property data Record
 * @constructor
 */
class ShopAcpInfoDialog(context: Context, var data: Record) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_acpshopinfo_popup
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        ed_mm.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_mm.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_mm.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        ed_sdId.setText(data.sale.toString() )
        tv_yuehuan.setText(data.unit.toString())
        if(data.sale.compareTo(BigDecimal("11"))== -1){

        }else{
            ed_purchase.setText(data.sale.intValueExact().toString())
            ed_purchase.setLongClickable(false);
            ed_purchase.setTextIsSelectable(false);
            ed_purchase.setEnabled(false)
            ed_purchase.setFocusable(false);
            tv_all.visibility = View.GONE
            if (!TextUtils.isEmpty(ed_purchase.text.toString())) {
                val number: BigDecimal = BigDecimal(ed_purchase.text.toString())
                val price: BigDecimal = BigDecimal(data.unit.toString())
                val Pay_price = number.multiply(price)
                tv_Pay_price.setText(Pay_price.toString())


            }
        }
        val ss = SpannableString("客官，我真心想置换米粒，价格公道哦")
        val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#0030FF"))
        ss.setSpan(foregroundColorSpan, 7, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tv_title.setText(ss)
        if(data.id.equals(CommonAppConfig.getInstance().getUid())){
            tv_title.setText("当前店铺为自己的店铺，无法购买")
            return
        }
        tv_all.setOnClickListener({
            ed_purchase.setText(data.sale.intValueExact().toString())
        })
        ed_purchase.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!TextUtils.isEmpty(ed_purchase.text.toString())) {
                    val number: BigDecimal = BigDecimal(ed_purchase.text.toString())
                    val price: BigDecimal = BigDecimal(data.unit.toString())
                    val Pay_price = number.multiply(price)
                    tv_Pay_price.setText(Pay_price.toString())


                }else{
                    tv_Pay_price.setText("")
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

        })
        tv_confirm.setOnClickListener({
            if(TextUtils.isEmpty(tv_Pay_price.text.toString())){
                ToastUtil.show("请输入置换数量")
                return@setOnClickListener
            }
            val number: BigDecimal = BigDecimal(ed_purchase.text.toString())
            val available: BigDecimal = BigDecimal(data.sale.toString())
            val sdCouponsNum: BigDecimal = BigDecimal(tv_Pay_price.text.toString())

            if(number.compareTo(available)==1){
                ToastUtil.show("我不需要这么多")
            }else{
                if (TextUtils.isEmpty(ed_mm.text.toString())||ed_mm.text.toString().length!=6) {
                    ToastUtil.show("请输入交易密码")
                    return@setOnClickListener
                }
                val multiple = if (data.shopType == 1) 1 else 10
                if (Integer.parseInt(ed_purchase.text.toString()) % multiple !== 0) {

                    ToastUtil.show("数量需要是10的倍数")
                    return@setOnClickListener
                }
                tv_confirm.setClickable(false)
                ElvesHttpUtil.getBuyingBuy( number,ed_mm.text.toString(),sdCouponsNum,BigDecimal(data.id),object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        ToastUtil.show(msg)
                        if (code != 200) {
                            tv_confirm.setClickable(true)

                            return
                        }
                    dismiss()
                    }

                    override fun onError() {
                        super.onError()
                        tv_confirm.setClickable(true)
                    }
                })
            }


        })
        ElvesHttpUtil.getParadiseInfo( object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }

                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                tv_ks.setText("我有米粒："+ paradiseInfoBean.crystalOre.toString())



            }

            override fun onError() {
                super.onError()

            }
        })
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
            }
    }
    override fun onDestroy() {
        Glide.get(context).clearMemory()

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_TBUY)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        super.onDestroy()
    }
}